library(tidyverse)
library(cjoint)
library(cregg)
library(xtable)

###SET WORKING DIRECTORY and LOAD FILES ----


set.seed(12345678)

white_long <- read.csv("b_data/4_white_data_for_analysis.csv")
black_long <- read.csv("b_data/5_black_data_for_analysis.csv")


###Feature order----
c_order <- c("Faces", "Identifies", "Religion", "Background", "Language",
             "Spouse", "Education", "Profession", "Political")


###Turning features into factors----

black_long <- black_long %>% 
  mutate(Faces = factor(Faces, levels = c("White man", "White woman", "Light man", "Light woman", 
                                          "Medium man",   "Medium woman", "Brown man", 
                                          "Brown woman", "Black man", "Black woman"), 
                        ordered = is.ordered(Faces)))


black_long <- black_long %>%
  mutate(Background = factor(Background, levels = c("Legal immigrant",
                                                    "Undoc. immigrant",
                                                    "Naturalized citizen",
                                                    "US-born legal parents",
                                                    "US-born undoc. parents",
                                                    "US-born legal grandparents",
                                                    "US-born undoc. grandparents")))

black_long <- black_long %>%
  mutate(Identifies = factor(Identifies, levels = c("American",
                                                    "Latino",
                                                    "Hispanic",
                                                    "Mexican-American",
                                                    "Mexican",
                                                    "White",
                                                    "Black")))

black_long <- black_long %>%
  mutate(Language = factor(Language, levels = c("Only English",
                                                "Bilingual",
                                                "Limited English",
                                                "No English"),
                           ordered = is.ordered(Language)))

black_long <- black_long %>%
  mutate(Education = factor(Education, levels = c("High school dropout",
                                                  "High school graduate",
                                                  "Bachelor's degree",
                                                  "Advanced/graduate degree"),
                            ordered = is.ordered(Education)))

black_long <- black_long %>%
  mutate(Profession = factor(Profession, levels = c("Waiter", "Janitor",
                                                    "Sales manager", "Teacher",
                                                    "IT professional", "Doctor",
                                                    "Small business owner")))

black_long <- black_long %>%
  mutate(Religion = factor(Religion, levels = c("Presbyterian", "Evangelical",
                                                "Catholic", "Not religious")))

black_long <- black_long %>%
  mutate(Political = factor(Political, levels = c("Democrat", "Republican",
                                                  "Independent", "Not political")))

black_long <- black_long %>%
  mutate(Spouse = factor(Spouse, levels = c("Not married",
                                            "S. is American",
                                            "S. is Black",
                                            "S. is White",
                                            "S. is same as profile")))

black_long$amerimport_high <- as.factor(black_long$amerimport_high)

white_long <- white_long %>% 
  mutate(Faces = factor(Faces, levels = c("White man", "White woman", "Light man", "Light woman", 
                                          "Medium man",   "Medium woman", "Brown man", 
                                          "Brown woman", "Black man", "Black woman"), 
                        ordered = is.ordered(Faces)))


white_long <- white_long %>%
  mutate(Background = factor(Background, levels = c("Legal immigrant",
                                                    "Undoc. immigrant",
                                                    "Naturalized citizen",
                                                    "US-born legal parents",
                                                    "US-born undoc. parents",
                                                    "US-born legal grandparents",
                                                    "US-born undoc. grandparents")))

white_long <- white_long %>%
  mutate(Identifies = factor(Identifies, levels = c("American",
                                                    "Latino",
                                                    "Hispanic",
                                                    "Mexican-American",
                                                    "Mexican",
                                                    "White",
                                                    "Black")))

white_long <- white_long %>%
  mutate(Language = factor(Language, levels = c("Only English",
                                                "Bilingual",
                                                "Limited English",
                                                "No English"),
                           ordered = is.ordered(Language)))

white_long <- white_long %>%
  mutate(Education = factor(Education, levels = c("High school dropout",
                                                  "High school graduate",
                                                  "Bachelor's degree",
                                                  "Advanced/graduate degree"),
                            ordered = is.ordered(Education)))

white_long <- white_long %>%
  mutate(Profession = factor(Profession, levels = c("Waiter", "Janitor",
                                                    "Sales manager", "Teacher",
                                                    "IT professional", "Doctor",
                                                    "Small business owner")))

white_long <- white_long %>%
  mutate(Religion = factor(Religion, levels = c("Presbyterian", "Evangelical",
                                                "Catholic", "Not religious")))

white_long <- white_long %>%
  mutate(Political = factor(Political, levels = c("Democrat", "Republican",
                                                  "Independent", "Not political")))

white_long <- white_long %>%
  mutate(Spouse = factor(Spouse, levels = c("Not married",
                                            "S. is American",
                                            "S. is Black",
                                            "S. is White",
                                            "S. is same as profile")))

white_long$amerimport_high <- as.factor(white_long$amerimport_high)


#### Marginal Mean Differences of Perceptions of Americanness, by strength of American identity among White respondents ----

amerimport_diff_w <- mm_diffs(na.omit(white_long),
                              rating_r ~ Faces + Background
                              + Education + Profession
                              + Language + Political
                              + Religion + Spouse
                              + Identifies,
                              id = ~ caseid,
                              by = ~ amerimport_high,
                              weights = ~weight, 
                              feature_order = c_order)

# Figure 4 ----

amerimport_diff_w_p <- plot(amerimport_diff_w)

figure_4 <- amerimport_diff_w_p + ggplot2::theme_bw() + 
  theme(legend.position="none", plot.title= element_text(size=10, hjust=0.5)) +
  scale_color_manual(values = rep("black", 9)) +
  geom_point(size = 2) + 
  ggplot2::labs(x = "Estimated Difference Between Strong Identifiers and Weak Identifiers",
                title = "Marginal Mean Differences in Perceived Americanness \n by Strength of American Identity Among White Respondents") 

ggsave("c_figures/figure_4.pdf", width=8.5, height=11, plot = figure_4)

# Table A9 ----
amerimport_diff_w_table <- amerimport_diff_w[c(5:9)]
print.xtable(xtable(amerimport_diff_w_table), "d_tables/table_A9.tex", type = "latex", include.rownames=FALSE)

####Marginal Mean Differences of Perceptions of Americanness, by strength of American identity among Black respondents-----

amerimport_diff_b <- mm_diffs(na.omit(black_long),
                              rating_r ~ Faces + Background
                              + Education + Profession
                              + Language + Political
                              + Religion + Spouse
                              + Identifies,
                              id = ~ caseid,
                              by = ~ amerimport_high,
                              weights = ~weight,
                              feature_order = c_order)


## Figure A3 ----
amerimport_diff_b_p <- plot(amerimport_diff_b)

figure_A3 <- amerimport_diff_b_p + ggplot2::theme_bw() + 
  theme(legend.position="none", plot.title= element_text(size=10, hjust=0.5)) +
  scale_color_manual(values = rep("black", 9)) +
  geom_point(size = 2) + 
  ggplot2::labs(x = "Estimated Difference Between Strong Identifiers and Weak Identifiers",
                title = "Marginal Mean Differences in Perceived Americanness \n by Strength of American Identity Among Black Respondents") 

ggsave("c_figures/figure_A3.pdf", width=8.5, height=11, plot = figure_A3)

# Table A11 ----
amerimport_diff_b_table <- amerimport_diff_b[c(5:9)]
print.xtable(xtable(amerimport_diff_b_table), "d_tables/table_A11.tex", type = "latex", include.rownames=FALSE)


#### Marginal Mean Differences of Perceptions of Americanness, by strength of **Racial** identity among **White** respondents -----

white_long$raceimport_high <- as.factor(white_long$raceimport_high)
black_long$raceimport_high <- as.factor(black_long$raceimport_high)

raceimport_diff_w <- mm_diffs(na.omit(white_long),
                              rating_r ~ Faces + Background
                              + Education + Profession
                              + Language + Political
                              + Religion + Spouse
                              + Identifies,
                              id = ~ caseid,
                              by = ~ raceimport_high,
                              weights = ~weight,
                              feature_order= c_order)

#### Figure A4 ----
raceimport_diff_w_p <- plot(raceimport_diff_w)
figure_A4 <- raceimport_diff_w_p + ggplot2::theme_bw() + 
  theme(legend.position="none", plot.title= element_text(size=10, hjust=0.5)) +
  scale_color_manual(values = rep("black", 9)) +
  geom_point(size = 2) + 
  ggplot2::labs(x = "Estimated Difference Between Strong Identifiers and Weak Identifiers",
                title = "Marginal Mean Differences in Perceived Americanness \n by Strength of Racial Identity Among White Respondents") 

ggsave("c_figures/figure_A4.pdf", width=8.5, height=11, plot = figure_A4)


#### Table A12 -----
raceimport_diff_w_table <- raceimport_diff_w[c(5:9)]
print.xtable(xtable(raceimport_diff_w_table), "d_tables/table_A12.tex", type="latex", include.rownames=FALSE)

### Marginal Mean Differences of Perceptions of Americanness, by strength of **Racial** identity among **Black** respondents -----
### note: this code is necessary to produce figure 5 and table A10-----

raceimport_diff_b <- mm_diffs(na.omit(black_long),
                              rating_r ~ Faces + Background
                              + Education + Profession
                              + Language + Political
                              + Religion + Spouse
                              + Identifies,
                              id = ~ caseid,
                              by = ~ raceimport_high,
                              weights = ~weight, 
                              feature_order= c_order)

#### Figure 5 ----
raceimport_diff_b_p <- plot(raceimport_diff_b)
figure_5 <- raceimport_diff_b_p + ggplot2::theme_bw() + 
  theme(legend.position="none", plot.title= element_text(size=10, hjust=0.5)) +
  scale_color_manual(values = rep("black", 9)) +
  geom_point(size = 2) + 
  ggplot2::labs(x = "Estimated Difference Between Strong Identifiers and Weak Identifiers",
                title = "Marginal Mean Differences in Perceived Americanness \n by Strength of Racial Identity Among Black Respondents")

ggsave("c_figures/figure_5.pdf", width=8.5, height=11, plot = figure_5)

#### Table A10 ----
print(raceimport_diff_b)
raceimport_diff_b_table <- raceimport_diff_b[c(5:9)]
print.xtable(xtable(raceimport_diff_b_table), "d_tables/table_A10.tex", type = "latex", include.rownames=FALSE)

#### Clear environment ----

rm(list = ls())
